package com.station.client;

import com.google.gwt.user.client.ui.Image;
import com.station.client.proxies.PCoordinates;
import com.station.client.proxies.PPetrolPump;

import java.util.Random;


/**
 * Created by Jimmy87 on 15.12.13.
 */
public abstract class SimpleCar extends Image {

    private static int numberCounter = 0;
    private PCoordinates coordinates;
    private PCoordinates target;
    private PCoordinates priorityTarget;
    private PCoordinates nextStep;
    private PCoordinates prevStep;
    private int number; //debug only todo

    public int cellState = 2; //на дороге
    private State state;
    private Direction direction;
    private int queneNumber;

    private final String fuelType;
    private String[] urls = new String[]{
            "imgs/lambo",
            "imgs/nisa",
            "imgs/Ford",
            "imgs/dodge",
            "imgs/bentley",
            "imgs/mazda6",
            "imgs/mazda_mp6",
            "imgs/t50"};       //массив с разными машинами

    //visual part
    //
    public int movingCounter = 0;
    public int widgetLeftPos = 0;
    public int widgetTopPos = 0;//todo
    public String url;
    private int cellSize = 50;
    public boolean mustBeDeleted = false;


    public PCoordinates getMainTarget() {      //devep only
        return target;
    }
    public SimpleCar(PCoordinates coordinates,String fuelType) {
        this.url = urls[new Random().nextInt(urls.length)];
        this.fuelType = fuelType;
        this.coordinates = coordinates;
        direction = Direction.Right;
        state = State.Standing;
        widgetLeftPos = coordinates.y * cellSize;
        widgetTopPos = coordinates.x * cellSize;
        changeImage();
        number = numberCounter;
        numberCounter++; //todo debug only
    }

    public int getQueneNumber() {
        return queneNumber;
    }

    public void setQueneNumber(int queneNumber) {
        this.queneNumber = queneNumber;
    }

    public boolean isReachTarget() {
        if (priorityTarget != null)
            return coordinates.equals(priorityTarget);
        return coordinates.equals(target);
    }

    public PCoordinates getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(PCoordinates coordinates) {
        this.coordinates = coordinates;
    }

    public PCoordinates getTarget() {
        if (priorityTarget != null)
            return priorityTarget;
        return target;
    }

    public void setTarget(PCoordinates target) {
        this.target = target;
    }

    public PCoordinates getNextStep() {
        return nextStep;
    }

    public void doStep() {
        if (nextStep == null)
            throw new RuntimeException("dump it!! empty step");
        prevStep = coordinates;
        coordinates = nextStep;
        nextStep = null;
    }

    public void setNextStep(PCoordinates nextStep) {
        this.nextStep = nextStep;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public String getFuelType() {
        return fuelType;
    }

    public void setPriorityTarget(PCoordinates priorityTarget) {
        this.priorityTarget = priorityTarget;
    }

    public PCoordinates getPrevStep() {
        return prevStep;
    }

    public boolean isPriorityTarget() {
        return priorityTarget != null;
    }

    public void setPrevStep(PCoordinates prevStep) {
        this.prevStep = prevStep;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public abstract boolean isProcFinished();

    public abstract void finishFilling(PCoordinates outPoint, PCoordinates exitPoint);



    public void changeImage() {
        switch (direction) {
            case Down : {
                setUrl(url + "_down.png");
                setWidth("35");
                setHeight("50");
            } break;
            case Up: {
                setUrl(url + "_up.png");
                setWidth("35");
                setHeight("50");
            } break;
            case Left:  {
                setUrl(url + "_left.png");
                setWidth("50");
                setHeight("35");
            } break;
            case Right:  {
                setUrl(url + "_right.png");
                setWidth("50");
                setHeight("35");
            } break;
        }

    }

}


